package com.jxmcloud.business.cut.controller.admin.activity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxmcloud.business.cut.constants.CutApi;
import com.jxmcloud.business.cut.dto.client.UserActivityInfoDto;
import com.jxmcloud.business.cut.model.query.*;
import com.jxmcloud.business.cut.service.database.*;
import com.jxmcloud.business.cut.util.ExcelExportUtil;
import com.jxmcloud.business.cut.vo.*;
import com.xlcloud.platform.util.RUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Web端砍一刀活动控制器
 */
@Validated
@Slf4j
@RestController
@RequestMapping(CutApi.NAH_CUT_ACTIVITY)
public class WebCutActivityController {

    @Resource
    private IUserActivityInfoDBService userActivityInfoService;

    @Resource
    private IUserInviteSuccessDBService inviteSuccessService;

    @Resource
    private IRewardPoolRecordDBService rewardPoolRecordService;

    @Resource
    private IGoodsPoolRecordDBService goodsPoolRecordService;


    @Resource
    private IGiftBuyRecordDBService giftBuyRecordService;


    /**
     * 查询用户活动信息列表
     * @param query 查询参数
     * @return 分页结果
     */
    @GetMapping("/getCutActivityPage")
    public String getCutActivityPage(UserActivityInfoQuery query) {
        IPage<UserActivityInfoVo> page = userActivityInfoService.getCutActivityPage(query);
        return RUtil.pageSuccessLAYUI(page.getRecords(), page.getTotal());
    }

    /**
     * 查询已成功邀请用户列表
     * @param query 查询参数
     * @return 分页结果
     */
    @GetMapping("/getInviteSuccessPage")
    public String getInviteSuccessPage(InviteSuccessQuery query) {
        IPage<InviteSuccessVo> page = inviteSuccessService.getInviteSuccessPage(query);
        return RUtil.pageSuccessLAYUI(page.getRecords(), page.getTotal());
    }

    /**
     * 查询奖金池记录列表
     * @param query 查询参数
     * @return 分页结果
     */
    @GetMapping("/getRewardPoolRecordPage")
    public RewardPoolRecordPageVo getRewardPoolRecordPage(RewardPoolRecordQuery query) {
        return rewardPoolRecordService.getRewardPoolRecordPage(query);
    }


    /**
     * 查询商品池记录列表
     * @param query 查询参数
     * @return 分页结果
     */
    @GetMapping("/getGoodsPoolRecordPage")
    public String  getGoodsPoolRecordPage(GoodsPoolRecordQuery query) {
        IPage<GoodsPoolRecordVo> page = goodsPoolRecordService.getGoodsPoolRecordPage(query);
        return RUtil.pageSuccessLAYUI(page.getRecords(), page.getTotal());
    }

    /**
     * 查询礼包购买记录列表
     * @param query 查询参数
     * @return 分页结果
     */
    @GetMapping("/getGiftBuyRecordPage")
    public String getGiftBuyRecordPage(GiftBuyRecordQuery query) {
        IPage<GiftBuyRecordVo> page = giftBuyRecordService.getGiftBuyRecordPage(query);
        return RUtil.pageSuccessLAYUI(page.getRecords(), page.getTotal());
    }
    /**
     * 导出商品池记录列表
     * @param query 查询参数
     * @param response HTTP响应
     * @throws IOException IO异常
     */
    @PostMapping("/exportGoodsPoolRecords")
    public void exportGoodsPoolRecords(GoodsPoolRecordQuery query, HttpServletResponse response) throws IOException {
        // 不分页，查询所有符合条件的记录
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        // 禁用分页计数，提高性能
        query.setSearchCount(false);
        IPage<GoodsPoolRecordVo> page = goodsPoolRecordService.getGoodsPoolRecordPage(query);
        List<GoodsPoolRecordVo> records = page.getRecords();
        // 导出Excel
        ExcelExportUtil.exportGoodsPoolRecords(response, records, "商品池记录");
        System.out.println("导出成功");
    }

    /**
     * 导出砍一刀活动记录
     * @param query 查询参数
     * @param response HTTP响应
     * @throws IOException IO异常
     */
    @PostMapping("/exportUserActivityRecords")
    public void exportUserActivityRecords(UserActivityInfoQuery query, HttpServletResponse response) throws IOException {
        // 不分页，查询所有符合条件的记录
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        // 禁用分页计数，提高性能
        query.setSearchCount(false);
        IPage<UserActivityInfoVo> page = userActivityInfoService.getCutActivityPage(query);
        List<UserActivityInfoVo> records = page.getRecords();

        // 导出Excel
        ExcelExportUtil.exportUserActivityRecords(response, records, "砍一刀活动记录");
        System.out.println("导出成功");
    }
    /**
     * 导出奖金池记录列表
     * @param query 查询参数
     * @param response HTTP响应
     * @throws IOException IO异常
     */
    @PostMapping("/exportRewardPoolRecords")
    public void exportRewardPoolRecords(RewardPoolRecordQuery query, HttpServletResponse response) throws IOException {
        // 不分页，查询所有符合条件的记录
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        // 禁用分页计数，提高性能
        query.setSearchCount(false);

        // 获取所有记录
        RewardPoolRecordPageVo pageVo = rewardPoolRecordService.getRewardPoolRecordPage(query);
        List<RewardPoolRecordVo> records = pageVo.getPage().getRecords();

        // 导出Excel
        ExcelExportUtil.exportRewardPoolRecords(response, records, "奖金池记录");
        System.out.println("导出成功");
    }

    /**
     * 导出拉新记录列表
     * @param query 查询参数
     * @param response HTTP响应
     * @throws IOException IO异常
     */
    @PostMapping("/exportInviteRecords")
    public void exportInviteRecords(InviteSuccessQuery query, HttpServletResponse response) throws IOException {
        // 不分页，查询所有符合条件的记录
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        // 禁用分页计数，提高性能
        query.setSearchCount(false);

        IPage<InviteSuccessVo> page = inviteSuccessService.getInviteSuccessPage(query);
        List<InviteSuccessVo> records = page.getRecords();

        // 导出Excel
        ExcelExportUtil.exportInviteRecords(response, records, "拉新记录");
        System.out.println("导出成功");
    }

    /**
     * 导出礼包购买记录列表
     * @param query 查询参数
     * @param response HTTP响应
     * @throws IOException IO异常
     */
    @PostMapping("/exportGiftBuyRecords")
    public void exportGiftBuyRecords(GiftBuyRecordQuery query, HttpServletResponse response) throws IOException {
        // 不分页，查询所有符合条件的记录
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        // 禁用分页计数，提高性能
        query.setSearchCount(false);

        IPage<GiftBuyRecordVo> page = giftBuyRecordService.getGiftBuyRecordPage(query);
        List<GiftBuyRecordVo> records = page.getRecords();

        // 导出Excel
        ExcelExportUtil.exportGiftBuyRecords(response, records, "礼包购买记录");
        System.out.println("导出成功");
    }


    /**
     * 修改用户活动信息
     * @param dto 更新信息DTO
     * @return 操作结果
     */
    @PutMapping("/updateUserActivityInfoForWeb")
    public String updateUserActivityInfo(@RequestBody UserActivityInfoDto dto) {
        userActivityInfoService.updateUserActivityInfoForWeb(dto);
        return RUtil.success();
    }
    /**
     * 删除用户活动信息
     * @param id 记录ID
     * @return 操作结果
     */
    @DeleteMapping("/deleteUserActivityInfo")
    public String deleteUserActivityInfo(@RequestParam Integer id) {
        userActivityInfoService.deleteUserActivityInfo(id);
        return RUtil.success();
    }

    /**
     * GM操作重置到缓存预留接口
     * @return 操作结果
     */
    @PostMapping("/ResetToCache")
    public String ResetToCache() {
        return RUtil.success();
    }
} 